<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>管理员登录 - 微信进件管理后台</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
        }
        
        .login-container {
            width: 400px;
            background: rgba(255, 255, 255, 0.95);
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            backdrop-filter: blur(10px);
        }
        
        .login-title {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
            font-size: 24px;
            font-weight: 300;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-input {
            width: 100%;
            height: 45px;
            line-height: 45px;
            border-radius: 5px;
            border: 1px solid #e6e6e6;
            padding: 0 15px;
            font-size: 14px;
            transition: border-color 0.3s;
        }
        
        .form-input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 5px rgba(102, 126, 234, 0.3);
        }
        
        .captcha-container {
            display: flex;
            align-items: center;
        }
        
        .captcha-container .form-input {
            flex: 1;
            margin-right: 10px;
        }
        
        .captcha-img {
            width: 120px;
            height: 45px;
            border-radius: 5px;
            cursor: pointer;
            border: 1px solid #e6e6e6;
        }
        
        .login-btn {
            width: 100%;
            height: 45px;
            line-height: 45px;
            border-radius: 5px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            color: white;
            font-size: 16px;
            cursor: pointer;
            transition: opacity 0.3s;
        }
        
        .login-btn:hover {
            opacity: 0.9;
        }
        
        .login-btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }
        
        .login-footer {
            text-align: center;
            margin-top: 20px;
            color: #666;
            font-size: 12px;
        }
        
        .error-msg {
            color: #f56565;
            font-size: 12px;
            margin-top: 5px;
            display: none;
        }
        
        .success-msg {
            color: #48bb78;
            font-size: 12px;
            margin-top: 5px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h2 class="login-title">管理员登录</h2>
        <form id="loginForm">
            <div class="form-group">
                <input type="text" name="username" class="form-input" placeholder="请输入用户名" required>
            </div>
            <div class="form-group">
                <input type="password" name="password" class="form-input" placeholder="请输入密码" required>
            </div>
            <div class="form-group">
                <div class="captcha-container">
                    <input type="text" name="captcha" class="form-input" placeholder="验证码">
                    <img src="/admin/captcha" class="captcha-img" id="captcha" 
                         title="点击刷新验证码" alt="验证码">
                </div>
            </div>
            <div class="form-group">
                <button type="submit" class="login-btn" id="loginBtn">登录</button>
                <div class="error-msg" id="errorMsg"></div>
                <div class="success-msg" id="successMsg"></div>
            </div>
        </form>
        <div class="login-footer">
            © 2024 微信进件管理系统
        </div>
    </div>
    
    <script>
        // 刷新验证码
        document.getElementById('captcha').addEventListener('click', function() {
            this.src = '/admin/captcha?' + Math.random();
        });
        
        // 登录表单提交
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            const loginBtn = document.getElementById('loginBtn');
            const errorMsg = document.getElementById('errorMsg');
            const successMsg = document.getElementById('successMsg');
            
            // 隐藏之前的消息
            errorMsg.style.display = 'none';
            successMsg.style.display = 'none';
            
            // 禁用按钮
            loginBtn.disabled = true;
            loginBtn.textContent = '登录中...';
            
            // 发送请求
            fetch('/admin/login', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.code == 1) {
                    successMsg.textContent = data.msg;
                    successMsg.style.display = 'block';
                    setTimeout(() => {
                        window.location.href = data.url || '/admin/dashboard';
                    }, 1000);
                } else {
                    errorMsg.textContent = data.msg;
                    errorMsg.style.display = 'block';
                    // 刷新验证码
                    document.getElementById('captcha').click();
                }
            })
            .catch(error => {
                errorMsg.textContent = '网络错误，请重试';
                errorMsg.style.display = 'block';
                document.getElementById('captcha').click();
            })
            .finally(() => {
                loginBtn.disabled = false;
                loginBtn.textContent = '登录';
            });
        });
        
        // 回车登录
        document.addEventListener('keydown', function(event) {
            if (event.keyCode === 13) {
                document.getElementById('loginBtn').click();
            }
        });
    </script>
</body>
</html>
